package com.nantai.jiafang.nantaijiafang.zhijian.fragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.blankj.utilcode.util.StringUtils;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.ViewModel.Zhijian_Net;
import com.nantai.jiafang.nantaijiafang.base.BaseFragment;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.AllDefects;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.InsDefect;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.QuiltDefects;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentSewingAlldefectsItemBinding;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentSewingDefectsBinding;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentZhijianAlldefectsItemBinding;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentZhijianDefectsBinding;
import com.nantai.jiafang.nantaijiafang.view.AddAndSubView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 提取疵点描述界面
 */
public class SewingDefectsFragment extends BaseFragment<FragmentSewingDefectsBinding> {
    private FragmentSewingDefectsBinding mbinding;
    private GridViewAdapter gridViewAdapter;
    private int sum;
    private String initstr;
    @Override
    public int setlayoutid() {
        return R.layout.fragment_sewing_defects;
    }
    @Override
    public void initUI(FragmentSewingDefectsBinding binding) {
        EventBus.getDefault().register(this);
        mbinding=binding;
        Zhijian_Net.getInsDefect(this.getContext().getApplicationContext(), new Zhijian_Net.GetSingData<List<InsDefect>>() {
            @Override
            public void success(List<InsDefect> s) {
                gridViewAdapter=new GridViewAdapter(getContext(),s);
                if(!StringUtils.isEmpty(initstr)){
                    gridViewAdapter.setDefects(initstr);

                }
                binding.gridViewDefects.setAdapter(gridViewAdapter);
            }
        });
        mbinding.btnDefectsBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        mbinding.btnDefectsEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (gridViewAdapter!=null){
//                   if (gridViewAdapter.getsum()==sum && gridViewAdapter.getresult().length()>0){
//                       ZhijianFragment_detail_add fragment=(ZhijianFragment_detail_add)getActivity().getSupportFragmentManager().findFragmentByTag("ZhijianFragment_detail_add");
//                       fragment.setdefectDes(gridViewAdapter.getresult());
//                       fragment.setADefectNo(gridViewAdapter.getsum()+"");
//                       getFragmentManager().popBackStack();
//                   }else{
//                       Toast.makeText(getContext(), "实疵点数与选择的点数不匹配！", Toast.LENGTH_SHORT).show();
//                   }
                    SewingAddFragment fragment=(SewingAddFragment)getActivity().getSupportFragmentManager().findFragmentByTag("SewingAddFragment");
                    fragment.setdefectDes(gridViewAdapter.getresult());
                    fragment.setADefectNo(gridViewAdapter.getsum()+"");
                    getFragmentManager().popBackStack();
                }
            }
        });
    }
    public class GridViewAdapter extends BaseAdapter {
        public LayoutInflater inflater;
        public List<InsDefect> defects;
        public List<Integer> results=new ArrayList<>();
        public GridViewAdapter(Context context, List<InsDefect> defects) {
            inflater = LayoutInflater.from(context);
            this.defects = defects;
            for (int i = 0; i < defects.size(); i++) {
                results.add(0);
            }
        }
        @Override
        public int getCount() {
            return defects.size();
        }

        @Override
        public InsDefect getItem(int position) {
            return defects.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            InsDefect currStr = getItem(position);
            View view;
            FragmentSewingAlldefectsItemBinding fragmentSewingAlldefectsItemBinding;

           if(convertView == null){
                LayoutInflater inflater=LayoutInflater.from(getContext());
                fragmentSewingAlldefectsItemBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_sewing_alldefects_item,parent,false);
                view=fragmentSewingAlldefectsItemBinding.getRoot();
                fragmentSewingAlldefectsItemBinding.setInsDefect(currStr);
                view.setTag(fragmentSewingAlldefectsItemBinding);
                if(!StringUtils.isEmpty(defects.get(position).getDNum())){
                    fragmentSewingAlldefectsItemBinding.Defectsnumber.setNum(Integer.parseInt(defects.get(position).getDNum()));
                    results.set(position,Integer.parseInt(defects.get(position).getDNum()));
                }else{
                    results.set(position,0);
                }

                fragmentSewingAlldefectsItemBinding.Defectsnumber.setOnNumChangeListener(new AddAndSubView.OnNumChangeListener() {
                    @Override
                    public void onNumChange(View view, int num) {
                        results.set(position,fragmentSewingAlldefectsItemBinding.Defectsnumber.getNum());
                    }
                });
            }else{

               view = convertView;
               fragmentSewingAlldefectsItemBinding = (FragmentSewingAlldefectsItemBinding) view.getTag();
          }

            return view;
        }


        public int getsum(){
            int sum=0;
            for (int i = 0; i < results.size(); i++) {
                sum+=results.get(i);
            }
            return sum;
        }
        public String getresult(){
            StringBuffer buffer=new StringBuffer();
            for (int i = 0; i < results.size(); i++) {
                if (results.get(i)!=0){
                    buffer.append(defects.get(i).getDCode()+"、"+defects.get(i).getDDescribe()+"*"+results.get(i).toString()+";");
                }
            }
            if(StringUtils.isEmpty(buffer.toString())){
                return "";
            }
            return buffer.toString().substring(0,buffer.toString().length()-1);
        }
        public void setDefects(String str){
            String[] items=str.split(";");
            for (String item:items
                 ) {
                try{
                    String name=item.split("、|\\*")[0];
                    String num=item.split("、|\\*")[2];
                    for (InsDefect item2 :defects ) {
                        if (item2.getDCode().equals(name)){
                            item2.setDNum(num);
                        }
                    }
                }catch (Exception e){

                }

            }
            notifyDataSetChanged();


        }

    }
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void SDefectNo(Integer num){
        sum=num.intValue();
    }
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void initDefectNo(String str){
        initstr=str;

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
